///
/// @ProjectName:    geejoan-tool
/// @Description:    错误处理
/// @File:           error.rs
/// @Author:         wujehy
/// @Email:          wujehy@qq.com
/// @Time:           2021-03-03 17:23
///
use std::{error, fmt, str};

#[derive(Clone, PartialEq, Eq)]
#[allow(dead_code)]
pub enum GeeError {
    ErrUnknow(),
    ErrorPassword(),
    ErrString(String),
}

impl fmt::Display for GeeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            GeeError::ErrUnknow() => write!(f, "未知的错误"),
            GeeError::ErrorPassword() => write!(f, "生成密码错误"),
            GeeError::ErrString(ref w) => write!(f, "{}", w),
        }
    }
}

impl fmt::Debug for GeeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl error::Error for GeeError {
    fn description(&self) -> &str {
        "description() is deprecated; use Display"
    }
    fn cause(&self) -> Option<&dyn error::Error> {
        None
    }
}

/// TODO from
impl From<String> for GeeError {
    fn from(s : String) -> GeeError {
        GeeError::ErrString(s)
    }
}
impl From<&str> for GeeError {
    fn from(s : &str) -> GeeError {
        GeeError::ErrString(s.to_string())
    }
}